package com.android.whereismy;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

/**
 * Overlay that indicate current location in address of target
 * @author akarae
 *
 */
public class MyLocationOverlay extends Overlay {
    
    Location myLocation;
    String provider;
    
    /**
     * MyLocationOverlay constructor.<BR>
     * 
     * @param myLocation
     * @param myAddress
     */
    public MyLocationOverlay (Location myLocation, String provider) {
        this.myLocation = myLocation;
        this.provider = provider;
    }

    /**
     * override draw.
     * 
     * @param canvas
     * @param mpaView
     * @param shadow
     */
    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        
        if (shadow == false) {
            // Get the current location
            Double latitude = myLocation.getLatitude()*1E6;
            Double longitude = myLocation.getLongitude()*1E6;
            
            GeoPoint geoPoint = new GeoPoint(latitude.intValue(), longitude.intValue());
            // Convert the location to screen pixels
            Projection projection = mapView.getProjection();
            Point point = new Point();
            projection.toPixels(geoPoint, point);

            // provider
            Paint addrPaint = new Paint();
            addrPaint.setARGB(150, 0, 0, 0);
            addrPaint.setAntiAlias(true);
            RectF addrRectF = new RectF(
                    mapView.getWidth() - mapView.getWidth() + 10, 
                    mapView.getHeight() - mapView.getHeight() + 10, 
                    mapView.getWidth() - mapView.getWidth() + 170, 
                    mapView.getHeight() - mapView.getHeight() + 40);
            canvas.drawRoundRect(addrRectF, 5, 5, addrPaint);
            Paint addrTextPaint = new Paint();
            addrTextPaint.setARGB(255, 255, 255, 255);
            addrTextPaint.setTextSize(15);
            canvas.drawText(
                    "PROVIDER : " + provider, 
                    mapView.getWidth() - mapView.getWidth() + 20, 
                    mapView.getHeight() - mapView.getHeight() + 30, 
                    addrTextPaint);
            
            // cross pointer
            Paint topPaint = new Paint();
            topPaint.setARGB(255, 204, 2, 26);
            topPaint.setAntiAlias(true);
            // blue
            Paint leftPaint = new Paint();
            leftPaint.setARGB(255, 4, 112, 190);
            leftPaint.setAntiAlias(true);
            // green
            Paint rightPaint = new Paint();
            rightPaint.setARGB(255, 41, 133, 52);
            rightPaint.setAntiAlias(true);
            // yellow
            Paint bottomPaint = new Paint();
            bottomPaint.setARGB(255, 251, 229, 82);
            bottomPaint.setAntiAlias(true);
            
            Point tempPoint = new Point();
            projection.toPixels(mapView.getMapCenter(), tempPoint);
            
            // top
            RectF topRectF = new RectF(
                    tempPoint.x - 4,
                    tempPoint.y - 30,
                    tempPoint.x + 4,
                    tempPoint.y - 4);
            canvas.drawRoundRect(topRectF, 5, 5, topPaint);
            // left
            RectF leftRectF = new RectF(
                    tempPoint.x - 30,
                    tempPoint.y - 4,
                    tempPoint.x - 4,
                    tempPoint.y + 4);
            canvas.drawRoundRect(leftRectF, 5, 5, leftPaint);
            // right
            RectF rightRectF = new RectF(
                    tempPoint.x + 4,
                    tempPoint.y - 4,
                    tempPoint.x + 30,
                    tempPoint.y + 4);
            canvas.drawRoundRect(rightRectF, 5, 5, rightPaint);
            // bottom
            RectF bottomRectF = new RectF(
                    tempPoint.x - 4,
                    tempPoint.y + 4,
                    tempPoint.x + 4,
                    tempPoint.y + 30);
            canvas.drawRoundRect(bottomRectF, 5, 5, bottomPaint);
        }
        
        super.draw(canvas, mapView, shadow);
    }

    @Override
    public boolean onTap(GeoPoint point, MapView mapView) {
    return false;
    }
}
